package org.zfes.snowier.zbss.xzy.router.device;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.zfes.snowier.auth.shiro.util.SecurityUtil;
import org.zfes.snowier.core.data.DataSet;
import org.zfes.snowier.web.controller.BaseController;
import org.zfes.snowier.zbss.xzy.biz.config.model.ConfigCate;
import org.zfes.snowier.zbss.xzy.biz.config.service.IConfigCateService;
import org.zfes.snowier.zbss.xzy.biz.config.service.IConfigItemValueService;
import org.zfes.snowier.zbss.xzy.biz.device.model.Device;
import org.zfes.snowier.zbss.xzy.biz.device.service.IDeviceService;
import org.zfes.snowier.zbss.xzy.biz.dto.AIConfigItemValueDto;
import org.zfes.snowier.zbss.xzy.biz.solution.model.Algorithm;
import org.zfes.snowier.zbss.xzy.biz.solution.service.IAlgorithmService;

import com.google.common.collect.Lists;
@Controller("org.zfes.snowier.zbss.xzy.router.device.DeviceController") 
@RequestMapping("/manger/device")
public class DeviceController implements BaseController {

	
	@Autowired
	private IDeviceService deviceService;

	@Autowired
	private IConfigItemValueService aIConfigItemValueService;
	@Autowired
	private IConfigCateService configCateService;
	@Autowired
	private  IAlgorithmService algorithmService;
	
	  @RequestMapping(value="/loadDeviceDataSet", method = {RequestMethod.POST,RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE)
	    public @ResponseBody  Object loadDeviceDataSet(@RequestParam Map<String,Object> params){
		    DataSet dataSet=  deviceService.loadDeviceDataSet(params);
		    return ajaxQuery("dataSet", dataSet);
	    }
	  
	  
	@RequestMapping(value="/loadAlgorithmConfig", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody  Object loadAlgorithmConfig(String  algorithmCode,Long deviceId){
		Optional<Algorithm> algorithmOp= algorithmService.loadAlgorithmByCode(algorithmCode);
		if(!algorithmOp.isPresent()) {
			return ajaxQuery("itemValueList", Lists.newArrayList());
		}
		
		Optional<ConfigCate>  cateOp= configCateService.loadCateByAlgorithmId(algorithmOp.get().getId());
		
		List<AIConfigItemValueDto> itemValueList =  aIConfigItemValueService.loadAIConfigItemValue(cateOp.get().getId(),deviceId);
		return ajaxQuery("itemValueList", itemValueList);
	}
	  
	  
  
    @RequestMapping(value="/createDevice", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public  @ResponseBody Object createDevice(Device device){
    	//device.setTenantId(SecurityUtil.getTenantId());
		deviceService.createDevice(device);
    	return ajaxDoneSuccess("数据操作成功 ");
    }
	
    @RequestMapping(value="/loadDevice", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody  Object loadDevice(Long id){
		Optional<Device> ops = deviceService.loadDeviceById(id);
		return ajaxQuery("device",ops);
    }
	
    @RequestMapping(value="/updateDevice", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody  Object updateDevice(Device device){
		deviceService.updateDevice(device);
		return ajaxDoneSuccess("数据操作成功 ");
    }
	
    @RequestMapping(value="/deleteDevice", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody  Object deleteDevice(Long id){
		deviceService.deleteDevice(id);
		return ajaxDoneSuccess("数据操作成功 ");
    }
    
	
    @RequestMapping(value="/setDeviceStatus", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody  Object setDeviceStatus(Long id,Byte status){
		deviceService.setDeviceStatus(id, status);
		return ajaxDoneSuccess("数据操作成功 ");
    }

}